package com.my.attendance.controller;

import com.my.attendance.entity.Employee;
import com.my.attendance.entity.EmployeeDTO;
import com.my.attendance.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/EmployeePageListServlet")
public class EmployeePageListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String pageNo = req.getParameter("pageNo");
            if(pageNo==null){
                pageNo="1";
            }
            EmployeeService employeeService = new EmployeeService();
            List<EmployeeDTO> employeePageList = employeeService.getEmployeePageList(Integer.parseInt(pageNo));
            req.setAttribute("employeePageList",employeePageList);
            int count = employeeService.getCount();
            req.setAttribute("count",count);
//            System.out.println(count);
//            System.out.println(employeePageList);
            int totalPage = (int) Math.ceil(count / 5.0);
            req.setAttribute("totalPage",totalPage);
            req.setAttribute("pageNo",Integer.parseInt(pageNo));
            req.getRequestDispatcher("jsp/admin/employee_list.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
